import axios from 'axios';

const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了，暂时不使用了
    // baseURL: 'https://www.easy-mock.com/mock/592501a391470c0ac1fab128',
    // baseURL: '/api',
    baseURL: process.env.VUE_APP_BASE_URL,
    // baseURL: process.env.VUE_APP_URL,
    timeout: 500000
});

//request 拦截器
//自请求发送前进行处理
service.interceptors.request.use(
    config => {
        config.headers['Content-Type'] = 'application/json;charset=utf-8';
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

//response 拦截器
//在接口响应后统一处理
service.interceptors.response.use(
    response => {
        let res = response.data;
        if (response.status === 200) {
            //如果返回的是文件
            if(response.config.responseType === 'blob') {
                return res;
            }
            //兼容返回的字符串数据
            if(typeof res === 'string') {
                res = res?JSON.parse(res) : res;
            }
            return res;
        } else {
            Promise.reject();
        }
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

let http = {};
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */

http.get = function(url) {
    return new Promise((resolve, reject) => {
      service
        .get(url)
        .then((res) => {
          resolve(res.data);
        })
        .catch((e) => {
          console.log(e);
          reject(e.data);
        });
    });
  };

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */

http.post = function(url, params) {
    console.log(params)
    return new Promise((resolve, reject) => {
      service
        .post(url, params)
        .then((res) => {
          resolve(res.data);
        })
        .catch((e) => {
          console.log(e);
          reject(e.data);
        });
    });
  };

export default service;
